﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using DigiLib;
using System.IO;

public partial class controls_admin_AdvertisingEdit : Admin_Controls_Base
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.removeHGC(divMessage);
        if (!IsPostBack)
        {
            base.ModuleByContent(ddlModule, "ADVERTISING", "THUMB_IMAGE", 1);
            if (ddlModule.Items.Count == 0)
                ddlModule.Items.Add(new ListItem("Chưa có module quảng cáo nào", "-1"));
            loadAdv();
        }
    }
    private void loadAdv()
    {
        try
        {
            ObjDT = BO_portal_ads_pictures.Search(Id);
            if (ObjDT != null && ObjDT.Rows.Count > 0)
            {
                if (!ObjDT.Rows[0]["PICTURE_PATH"].ToString().Equals(string.Empty))
                {
                    string extension = Path.GetExtension(ObjDT.Rows[0]["PICTURE_PATH"].ToString());
                    if (extension == ".swf")
                        imgAdv.InnerHtml = "<object height=' 100' border='0' width='200' codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"><param value=\"" + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "\" name=\"movie\"><param value=\"always\" name=\"AllowScriptAccess\"><param value=\"High\" name=\"quality\"><param value=\"transparent\" name=\"wmode\"><param value=\"#\" name=\"FlashVars\"><embed  height=' 100' border='0' width='300' allowscriptaccess=\"always\" wmode=\"transparent\" loop=\"true\" play=\"true\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" flashvars=\"#\" src=\"" + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "\"></object>";
                    else imgAdv.InnerHtml = "<img src='/" + Setting.Path + ObjDT.Rows[0]["PICTURE_PATH"].ToString() + "' height='100' onerror='ImgError(this);' />";
                }
                txtTitle.Text = ObjDT.Rows[0]["PICTURE_NAME"].ToString();
                txtUrl.Text = ObjDT.Rows[0]["TARGET_LINK"].ToString();
                //txtExpired.Text = Convert.ToDateTime(ObjDT.Rows[0]["END_DATE"]).ToString("MM/dd/yyyy");
                //txtStart.Text = Convert.ToDateTime(ObjDT.Rows[0]["START_DATE"]).ToString("MM/dd/yyyy");
                chbDisplay.Checked = Convert.ToBoolean(ObjDT.Rows[0]["IS_DISPLAY_ON_HOME_PAGE"]);
                txtSortDisplay.Text = ObjDT.Rows[0]["ORDER_DISPLAY"].ToString();
                ddlModule.SelectedValue = ObjDT.Rows[0]["MODULE_ID"].ToString();
            }
        }
        catch (Exception ex) { divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi trong quá trình xử lý<br />" + ex.Message); }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        ObjDT = BO_portal_ads_pictures.Search(Id);
        string fileName = String.Empty;
        fileName = base.Upload("advertising", ObjDT.Rows[0]["PICTURE_PATH"].ToString(), fulAdv, divMessage, false);
        try
        {
            string title = txtTitle.Text.Trim();
            string url = txtUrl.Text.Trim();
            int module_id = Convert.ToInt32(ddlModule.SelectedValue);
            bool display = chbDisplay.Checked;
            int sortDisplay = Convert.ToInt32(txtSortDisplay.Text);
            DateTime endDate = DateTime.Now;
            //DateTime.TryParse(txtExpired.Text.Trim(), out endDate);
            DateTime startDate = DateTime.Now;
            //DateTime.TryParse(txtStart.Text.Trim(), out startDate);
            string result = BO_portal_ads_pictures.Update(Id, module_id, title
                    , fileName, url, display, startDate, endDate, sortDisplay, Convert.ToDateTime(ObjDT.Rows[0]["CREATED_DATE"]), ObjDT.Rows[0]["CREATED_BY"].ToString(), DateTime.Now, Administrator.USER_NAME, true);
            if (result.Equals("0"))
                divMessage.InnerHtml = MessageBox.Error("Thông Báo", "Lỗi trong quá trình truyền tải dữ liệu. Mong bạn trở lại sau .");
            else
            {
                divMessage.InnerHtml = MessageBox.Success("Thông Báo", "Thực hiện thành công .");
                Response.Redirect("Default.aspx?control=Advertising&keyword=" + Keyword);
            }
        }
        catch (Exception ex)
        {
            divMessage.InnerHtml = MessageBox.Error("Lỗi", "Phát hiện lỗi xảy ra !<br />" + ex.Message);
        }
    }
}
